package com.kiragame.xchat.fragment;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.kiragame.xchat.ChatActivity;
import com.kiragame.xchat.R;
import com.kiragame.xchat.adapter.ChatRoomAdapter;
import com.kiragame.xchat.adapter.UserAdapter;
import com.kiragame.xchat.object.MessageContent;
import com.kiragame.xchat.object.User;
import com.kiragame.xchat.service.ChatService;
import com.quentindommerc.superlistview.SuperListview;

public class ChatRoomFragment extends Fragment {

	private static final String ARG_POSITION = "position";

	private int position;

	private LinearLayout llMain;
	private SuperListview lvChatRoom;
	private LinearLayout llSendMessage;
	private EditText etMessage;
	private Button btSend;
	private LinearLayout llSliding;
	private SuperListview lvUsers;

	// CHAT IN ROOM
	private List<MessageContent> messageContentData = new ArrayList<MessageContent>();
	private ChatRoomAdapter chatAdapter;

	// USER IN ROOM
	private List<User> userData = new ArrayList<User>();
	private UserAdapter userAdapter;

	public static ChatRoomFragment newInstance() {
		ChatRoomFragment f = new ChatRoomFragment();
		// Bundle b = new Bundle();
		// b.putInt(ARG_POSITION, position);
		// f.setArguments(b);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// position = getArguments().getInt(ARG_POSITION);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_room, container, false);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		chatAdapter = new ChatRoomAdapter(getActivity(), messageContentData);
		lvChatRoom.setAdapter(chatAdapter);

		userAdapter = new UserAdapter(getActivity(), userData);
		lvUsers.setAdapter(userAdapter);
	}

	private void setEvent() {
		btSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String message = etMessage.getText().toString();
				//ChatService.getInstance().sendMessageChatRoom(message);
			}
		});

		lvChatRoom.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				MessageContent messageContent = (MessageContent) parent.getAdapter().getItem(position);
				chatToUser(Integer.toString(messageContent.getFromUserID()));
			}
		});
		
		lvUsers.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				User user = (User) parent.getAdapter().getItem(position);
				chatToUser(Integer.toString(user.getUserID()));
			}
		});
	}

	protected void chatToUser(String userID) {
		Intent intent = new Intent(getActivity(), ChatActivity.class);
		intent.putExtra(ChatActivity.TO_USER, userID);
		startActivity(intent);
	}

	private void findViewIds(View view) {
		llMain = (LinearLayout) view.findViewById(R.id.ll_main);
		lvChatRoom = (SuperListview) view.findViewById(R.id.lv_chat_room);
		llSendMessage = (LinearLayout) view.findViewById(R.id.ll_send_message);
		etMessage = (EditText) view.findViewById(R.id.et_message);
		btSend = (Button) view.findViewById(R.id.bt_send);
		llSliding = (LinearLayout) view.findViewById(R.id.ll_sliding);
		lvUsers = (SuperListview) view.findViewById(R.id.lv_users);
	}

	public void updateListMessage(String message) {
		if (message != null) {
			messageContentData.add(MessageContent
					.fromJsonToMessageContent(message));
		}
		chatAdapter.notifyDataSetChanged();
	}

	public void updateListUserInRoom(String message) {
		if (message != null) {
			userData.addAll(User.fromJsonArrayToUsers(message));
		}
		userAdapter.notifyDataSetChanged();
	}

	public void onUserEnteredRoom(String id) {
		for (User user : userData) {
			if (id.equals(Integer.toString(user.getUserID()))) {
				return;
			}
		}
		List<String> userIDs = new ArrayList<String>();
		userIDs.add(id);
		ChatService.getInstance().getUserByIds(userIDs, 1, 50);
	}

	public void onUserLeftRoom(String id) {
		Iterator<User> user = userData.iterator();
		while (user.hasNext()) {
			User mUser = (User) user.next();
			if (id.equals(Integer.toString(mUser.getUserID()))) {
				user.remove();
			}
		}
		userAdapter.notifyDataSetChanged();
	}

}
